package com.ruoyi.system.service.impl;

import com.google.common.util.concurrent.RateLimiter;
import com.ruoyi.system.service.TwCommonService;
import com.ruoyi.system.service.TwTweetsPullService;
import com.twitter.clientlib.ApiException;
import com.twitter.clientlib.api.TwitterApi;
import com.twitter.clientlib.model.GenericTweetsTimelineResponse;
import com.twitter.clientlib.model.Tweet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class TwTweetsPullServiceImpl implements TwTweetsPullService {

    // 每秒限制1个令牌桶
    private RateLimiter rateLimiter = RateLimiter.create(1.0);

    @Autowired
    private TwCommonService twCommonService;

    /**
     * 根据用户ID获取其推文列表，每次100条。
     * 按照时间线倒序，只能获取最近的3200条
     *
     * @param userId
     * @param paginationToken 分页的下一个token
     */
    public GenericTweetsTimelineResponse fetchTweetsByUserId(String userId, String paginationToken) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        GenericTweetsTimelineResponse result = new GenericTweetsTimelineResponse();
        TwitterApi twitterApi = twCommonService.getTwitterApiByBearerToken();
        // Set the params values
//        String id = "id_example"; // String | The ID of the User to list Tweets of
//        String sinceId = "791775337160081409"; // String | The minimum Tweet ID to be included in the result set. This parameter takes precedence over start_time if both are specified.
//        String untilId = "1346889436626259968"; // String | The maximum Tweet ID to be included in the result set. This parameter takes precedence over end_time if both are specified.
        Integer maxResults = 100; // Integer | The maximum number of results
        Set<String> exclude = new HashSet<>(Arrays.asList()); // Set<String> | The set of entities to exclude (e.g. 'replies' or 'retweets')
//        String paginationToken = "paginationToken_example"; // String | This parameter is used to get the next 'page' of results.
//        OffsetDateTime startTime = OffsetDateTime.parse("2021-02-01T18:40:40.000Z"); // OffsetDateTime | YYYY-MM-DDTHH:mm:ssZ. The earliest UTC timestamp from which the Tweets will be provided. The since_id parameter takes precedence if it is also specified.
//        OffsetDateTime endTime = OffsetDateTime.parse("2021-02-14T18:40:40.000Z"); // OffsetDateTime | YYYY-MM-DDTHH:mm:ssZ. The latest UTC timestamp to which the Tweets will be provided. The until_id parameter takes precedence if it is also specified.
        Set<String> expansions = new HashSet<>(Arrays.asList()); // Set<String> | A comma separated list of fields to expand.
        Set<String> tweetFields = new HashSet<>(Arrays.asList(
                "id", "created_at", "text", "author_id", "public_metrics"
        )); // Set<String> | A comma separated list of Tweet fields to display.
        Set<String> userFields = new HashSet<>(Arrays.asList()); // Set<String> | A comma separated list of User fields to display.
        Set<String> mediaFields = new HashSet<>(Arrays.asList()); // Set<String> | A comma separated list of Media fields to display.
        Set<String> placeFields = new HashSet<>(Arrays.asList()); // Set<String> | A comma separated list of Place fields to display.
        Set<String> pollFields = new HashSet<>(Arrays.asList()); // Set<String> | A comma separated list of Poll fields to display.
        try {
                result = twitterApi.tweets().usersIdTweets(
                        userId,
                        null,
                        null,
                        maxResults,
                        exclude,
                        paginationToken,
                        null,
                        null,
                        expansions,
                        tweetFields,
                        userFields,
                        mediaFields,
                        placeFields,
                        pollFields);
                System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling TweetsApi#usersIdTweets");
            System.err.println("Status code: " + e.getCode());
            System.err.println("Reason: " + e.getResponseBody());
            System.err.println("Response headers: " + e.getResponseHeaders());
            e.printStackTrace();
        }
        return result;
    }


}
